package cn.com.bsoft.cloud.utils.test;

import java.security.MessageDigest;


public class SecuritySHA1Utils {

    /**
     * @Comment SHA1实现
     * @Author Ron
     * @Date 2017年9月13日 下午3:30:36
     * @return
     */
    public static String shaEncode(String inStr) throws Exception {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }


    public static String getSignature(String appid, String timestamp, String nonce, String token) throws Exception{

        String[] arrTmp={timestamp,token,nonce,appid};
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<arrTmp.length;i++){
            sb.append(arrTmp[i]);
        }
        String s1 =sb.toString();
        String tmpStr =SecuritySHA1Utils.shaEncode(s1);

        return tmpStr;

    }

    public static void main(String args[]) throws Exception {
        String appId="OX190418000102";
        String timess=DateUtil.timeStamp();
        String timestamp = DateUtil.timeStampToDotNet(timess);
        String nonce="8217";
        String token="6428e288-dab1-f5d5-c883-acaa9b88f57f";
        String date = DateUtil.timeStamp2Date(timess, "yyyy-MM-dd HH:mm:ss");
        String str = SecuritySHA1Utils.getSignature(appId,timestamp,nonce,token);
        System.out.println("时间戳：" + timestamp);
        System.out.println("原始：" + str);
        System.out.println("SHA后：" + shaEncode(str));
        System.out.println("date：" + date);
    }
}


